#ifndef STRING_DEMO_HPP
#define STRING_DEMO_HPP

#include <string>
#include <vector>

#include <iostream>
#include <sstream>
#include <stdio.h>

int find_string(const std::string &str, const std::string &sub_str,
                std::vector<int> &result) {
  if (str.empty() || sub_str.empty()) {
    return false;
  }
  if (str.size() < sub_str.size()) {
    return false;
  }

  std::size_t i = 0, j = 0;
  for (i = 0; i < str.size(); i++) {
    std::size_t it = i;
    j = 0;
    while (str[it] == sub_str[j]) {
      ++j;
      ++it;
      if (j == sub_str.size()) {
        result.push_back(it - sub_str.size());
        // return it - sub_str.size();
        break;
      }
    }
  }

  return true;
}

int find_string(const std::string &str, const std::string &sub_str) {
  if (str.empty() || sub_str.empty()) {
    return false;
  }
  if (str.size() < sub_str.size()) {
    return false;
  }

  std::size_t i = 0, j = 0;
  for (i = 0; i < str.size(); i++) {
    std::size_t it = i;
    j = 0;
    while (str[it] == sub_str[j]) {
      ++j;
      ++it;
      if (j == sub_str.size()) {
        return it - sub_str.size();
      }
    }
    if (j > 0) {
      i += j - 1;
    }
  }

  return true;
}

using namespace std;

int test_string_buffer() {
  std::stringbuf buffer;
  buffer.str("This is test stringbuf");
  cout << buffer.str() << endl; // This is test stringbuf

  streamsize size = buffer.in_avail(); // long long可读字符个数=22
  cout << "1.1.size=" << size << endl;

  char arr[10] = {0}, arr1[23] = {0};
  buffer.sgetn(arr, 10);    //得到n个字符 This is te
  size = buffer.in_avail(); // 12
  cout << "1.2.size=" << size << endl;

  puts(arr); //从开头放10个字符,尾部换行
  // gets_s(arr1, 23);                //不要使用
  // cout << "arr1=" << arr1 << endl; //This is te孂?

  buffer.sgetn(arr1, 22);
  cout << "1.3.arr1=" << arr1 << endl;

  buffer.str("This is test stringbuf");
  buffer.sgetn(arr1, 22);
  cout << "1.4.arr1=" << arr1 << endl; // This is test stringbuf

  buffer.str("This is test stringbuf");
  buffer.sputn("Tom", 3);
  char arr2[25] = {0};
  buffer.sgetn(arr2, 25);
  cout << "1.5.arr2=" << arr2 << endl; // Toms is test stringbuf

  buffer.str("This is test stringbuf");
  cout << "1.6.";
  while (buffer.snextc() != EOF) //移动到下一个位置
  {
    char cc = buffer.sgetc(); //返回当前位置字符
    cout << cc << ";";
  }
  cout << endl; /*h;i;s; ;i;s; ;t;e;s;t; ;s;t;r;i;n;g;b;u;f; */
  size = buffer.in_avail();
  cout << "1.7.size=" << size << endl; // 0

  buffer.str("This is test stringbuf");
  cout << "1.8.";
  while (buffer.sgetc() != EOF) //移动到下一个位置
  {
    char cc = buffer.sbumpc(); //返回当前位置字符
    cout << cc << ";";
  }
  cout << endl;
  size = buffer.in_avail(); /*T; h; i; s; ; i; s; ; t; e; s; t; ; s; t; r; i; n;
                               g; b; u; f;*/
  cout << "1.9.size=" << size << endl; // 0

  buffer.str("This is test stringbuf");
}

#endif // STRING_DEMO_HPP